<?php
/**
 * Created by PhpStorm.
 * User: LinMin
 * Date: 2018/1/30
 * Time: 9:29
 */

namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class Menu
{
    public static function creaate($request){
        $id = DB::table('menu')->insertGetId(
            [
                'belone_uid' => $request->input('belone_uid'),
                'cate_name'=>$request->input('cate_name'),
                'cate_type'=>$request->input('cate_type'),
                'cate_url'=>$request->input('cate_url'),
                'cate_rate'=>$request->input('cate_rate')
            ]
        );
    }//创建目录
    public static function getMenu(){
        $menu_first=DB::table('menu')->where([
            ['cate_show',1]
        ])->orderBy('cate_rate', 'desc')->get();
        foreach ($menu_first as $k => $v){
            if($v->cate_type==1){
                $menu_first[$k]->url=$v->cate_url;
            }
            else if($v->cate_type==2)
            {
                $menu_first[$k]->url=$v->cate_url.'?page_name='.$v->uid.'&uid='.$v->uid;
            }

        }
        $menu=self::getTree($menu_first,0);
        return $menu;
    }//获取目录
    public static function getProductCate(){

        $menu=DB::table('menu')->where([['cate_show','=','2']])->orderBy('cate_rate', 'asc')->get();
        $tree = self::getTree($menu, 0);
        return $tree;

    }//获取商品分类

    public static function getName($cate_uid){
        return DB::table('menu')->where('uid',$cate_uid)->get()[0]->cate_name;
    }

    public static function getCateMenu($cate_show){
        $menu=DB::table('menu')->where([['cate_show','=',$cate_show]])->orderBy('cate_rate', 'asc')->get();
        $tree = self::getTree($menu, 0);
        return $tree;
    }

    public static function getTree($data, $pId)
    {
        $tree = [];
        foreach($data as $k => $v)
        {

            if($v->belone_uid == $pId)
            {        //父亲找到儿子

                $v->child = self::getTree($data, $v->uid);
                $tree[] = $v;
            }
        }
        return $tree;
    }//数组转数
    public static function getParentTree($data,$type){
        $tree=[];
        $parend=$data== null ?0 :$data;
        if($parend!=0)
        {
            do{
                $data=DB::table('menu')->where('uid',$parend)->where('cate_show',$type)->get();
                $data=$data[0];
                $parend=$data->belone_uid;
                $tree[]=$data;
            } while ($parend!=0);
        }

        return $tree;
    }//获取父节点，用于面包屑
}